package com.cskaoyan.user.service.impl;

import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.service.IUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import javax.swing.*;
import java.security.DigestException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class IUserRegisterServiceImp implements IUserRegisterService {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    JavaMailSender mailSender;

    @Override
    public int register(UserRegisterRequest request) {
        //1.查询是否存在
        Example example = new Example(Member.class);
        example.createCriteria().orEqualTo("username", request.getUserName())
                .orEqualTo("email", request.getEmail());
        List<Member> members = memberMapper.selectByExample(example);
        if (members.size() > 0) {
            return 1;
        }
        /**
         * 2.插入member数据
         */
        Member member = new Member();
        member.setUsername(request.getUserName());
        //密码加密
        String s = DigestUtils.md5DigestAsHex(request.getUserPwd().getBytes());
        member.setPassword(s);
        member.setEmail(request.getEmail());
        member.setCreated(new Date());
        member.setUpdated(new Date());
        member.setIsVerified("N");
        int i = memberMapper.insertMember(member);
        //int i = 1;
        if (i != 1) {
            //注册失败，服务器问题，返回2
            return 2;
        }
        //3.插入激活表
        UserVerify userVerify = new UserVerify();
        userVerify.setUsername(request.getUserName());
        //加密UUId
        String str = request.getUserName() + UUID.randomUUID().toString();
        userVerify.setUuid(str);
        userVerify.setRegisterDate(new Date());
        userVerify.setIsVerify("N");
        userVerify.setIsExpire("N");
        int j = userVerifyMapper.insertSelective(userVerify);
        if (j != 1) {
            return 2;
        }
        //4.发送邮件

        new Thread(new Runnable() {
            @Override
            public void run() {
                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                simpleMailMessage.setFrom("1109123947@qq.com");
                simpleMailMessage.setTo(request.getEmail());
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("http://localhost:8080/user/verify?username=").append(request.getUserName()).append("&uuid=").append(userVerify.getUuid());
                simpleMailMessage.setText(stringBuffer.toString());
                simpleMailMessage.setSubject("CSMALL");
                mailSender.send(simpleMailMessage);
            }
        }).start();
        return 0;
    }

    @Override
    public int verify(UserVerifyRequest request) {
        Example example = new Example(UserVerify.class);
        example.createCriteria().andEqualTo("username", request.getUsername())
                .andEqualTo("uuid", request.getUuid());
        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
        /**
         *  2 数据错误
         *  1 系统错误
         *  0 成功
         */
        if (userVerifies.size() != 1) {
            return 2;
        }

        UserVerify userVerify = userVerifies.get(0);
        userVerify.setIsVerify("Y");
        userVerify.setIsExpire("Y");
        int i = userVerifyMapper.updateByPrimaryKeySelective(userVerify);
        if (i != 1) {
            return 1;
        }

        Member member = memberMapper.selectByUsername(userVerify.getUsername());
        member.setIsVerified("Y");
        member.setUpdated(new Date());
        int j = memberMapper.updateByPrimaryKeySelective(member);
        if (j != 1) {
            return 1;
        }

        return 0;
    }
}
